<?php


require 'response.php';
include 'roles.php';
include 'user.php';
include 'milestones.php';   
include 'online.php';
include 'projects.php';
include 'tasklists.php';
include 'tasks.php';
include 'users.php';
include 'jsonResponse.php';
include 'xmlResponse.php';
 
function Validate($OWNER, $MAJOR, $MINOR, $BUILD, $TYPE, $MESSAGE)
{
    return ValidateResponseJSON(2, "Success!", $OWNER,$MAJOR, $MINOR, $BUILD, $TYPE, $MESSAGE);
     
}

function getDaysLeft($end)
{
        $tod = date("d.m.Y");
        $start = strtotime($tod);
        $diff = $end - $start;
        return floor($diff / 86400);
}

function getProgress($project)
{
        $project = mysql_real_escape_string($project);
        $project = (int) $project;

        $otasks = mysql_query("SELECT COUNT(*) FROM tasks WHERE project = $project AND status = 1");
        $otasks = mysql_fetch_row($otasks);
        $otasks = $otasks[0];

        $clotasks = mysql_query("SELECT COUNT(*) FROM tasks WHERE project = $project AND status = 0");
        $clotasks = mysql_fetch_row($clotasks);
        $clotasks = $clotasks[0];

        $totaltasks = $otasks + $clotasks;
        if ($totaltasks > 0 and $clotasks > 0) {
            $done = $clotasks / $totaltasks * 100;
            $done = round($done);
        } else {
            $done = 0;
        }
        return $done;
} 

function CheckSession($sessionEnabled) 
{
    //echo "SessionID: $sessionid versus ".session_id();
  
    if($sessionEnabled==true && !$_SESSION['username'])
    {
        return false;        
    }
    else 
    {
        return true;
    }
  
   
}

function CheckSessionAndPermissions($con, $db_name,$sessionEnabled, $requestVars, $branch,$action, $type)
{
    $permAction = $action;
    if($action == 'get' )
    {
     $permAction = 'edit'; // There is no GET permission.    
    }
    
    if($action == 'addUser')
    {
     $permAction = 'add'; // There is no addUser permission.    
    }
    
    
     if($sessionEnabled == false)
    {
        $s_uid = $requestVars['s_uid']; 
        $username = '(No Session Dummy User)';
        if(!$s_uid)
        {
            if($type == 'json')
            {            
                return GenericResponseJSON(3, "Sessions are disabled, you need to pass userid!");
            }
            else if($type == 'xml')
            {
                return GenericResponseXML(3, "Sessions are disabled, you need to pass userid!");
            }                            

           
        } 
    }
    else
    {
        $username = $_SESSION['username'];
        $s_uid =  $_SESSION['s_uid'];
    }
  
    if(CheckSession($sessionEnabled) == false)                
    {  
        if($type == 'json')
        {            
            return GenericResponseJSON(4, "User is not logged in, Please login! http://CollabtiveURL/mobile/login/ ");
        }
        else if($type == 'xml')
        {
            return GenericResponseXML(4, "User is not logged in, Please login! http://CollabtiveURL/mobile/login/ ");
        }       
        
    }
    else 
    {
        if(CheckRoll($con, $db_name, $s_uid, $branch, $permAction) == 0)
        {
            return NoPermission($username,$branch,$action, $type);
        }
        else 
        {  
            $requestVars['s_uid'] =  $s_uid;
            return $requestVars; 
        }
    }
}

 function sanitizeArray($inarr)
{
    if (!is_array($inarr))
    {
        $inarr = array();
    }
    if (empty($inarr["add"]))
    {
        $inarr["add"] = 0;
    }
    if (empty($inarr["edit"]))
    {
        $inarr["edit"] = 0;
    }
    if (empty($inarr["del"]))
    {
        $inarr["del"] = 0;
    }
    if (empty($inarr["close"]))
    {
        $inarr["close"] = 0;
    }
    if (empty($inarr["read"]))
    {
        $inarr["read"] = 0;
    }

    return (array) $inarr;
}

function getResponseType($type)
{
    if($type == 'XML' || $type == 'xml' || $type == 'x' || $type == 'X')
    {
        return 'xml';
    }
    else if($type == 'JSON' || $type == 'json' || $type == 'j' || $type == 'J')
    {
        return 'json';
    }
    else 
    {
        return 'json';
    }
    
    
    
    
}


?>
